Crate rand_regex[][src]

Generates strings are byte strings following rule of a regular expression.

use rand::{SeedableRng, Rng};

let mut rng = rand_xorshift::XorShiftRng::from_seed(*b"The initial seed");

// creates a generator for sampling strings
let gen = rand_regex::Regex::compile(r"\d{4}-\d{2}-\d{2}", 100).unwrap();

// sample a few strings randomly
let samples = (&mut rng).sample_iter(&gen).take(3).collect::<Vec<String>>();

// all Unicode characters are included when sampling
assert_eq!(samples, vec![
    "᱒᠔८᪄-꧒០-૭۰".to_string(),
    "𞅆٩𑄿꘡-᠐꤆-෧᪀".to_string(),
    "𑄹꯸२౦-9႑-७௮".to_string()
]);

// you could use `regex_syntax::Hir` to include more options
let mut parser = regex_syntax::ParserBuilder::new().unicode(false).build();
let hir = parser.parse(r"\d{4}-\d{2}-\d{2}").unwrap();
let gen = rand_regex::Regex::with_hir(hir, 100).unwrap();
let samples = (&mut rng).sample_iter(&gen).take(3).collect::<Vec<String>>();
assert_eq!(samples, vec![
    "8922-87-63".to_string(),
    "3149-18-88".to_string(),
    "5420-58-55".to_string(),
]);

Structs

EncodedString

A string together with its Encoding.

Regex

A random distribution which generates strings matching the specified regex.

Enums

Encoding

String encoding.

Error

Error returned by Regex::compile() and Regex::with_hir().